package cn.ai.shoes.core.utils;

import cn.ai.shoes.common.exception.ServiceException;
import cn.ai.shoes.common.exception.constant.GlobalErrorCodeConstants;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.aliyun.tea.TeaException;
import org.jasypt.util.text.StrongTextEncryptor;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 阿里云短信工具类
 *
 * @author itcast
 */
@Component
public class AliyunSmsUtils {

    public static final String PS = "MS142512N415!";
    public static String accessKeyId;
    public static String accessKeySecret;
    private static String signName;
    private static String templateCode;

    @Value("${aliyun.sms.accessKeyId}")
    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }
    @Value("${aliyun.sms.accessKeySecret}")
    public void setAccessKeySecret(String accessKeySecret) {
        this.accessKeySecret = accessKeySecret;
    }

    @Value("${aliyun.sms.signName}")
    public void setSignName(String signName) {
        AliyunSmsUtils.signName = signName;
    }
    @Value("${aliyun.sms.templateCode}")
    public void setTemplateCode(String templateCode) {
        AliyunSmsUtils.templateCode = templateCode;
    }

    public static com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
        // 工程代码建议使用更安全的无AK方式，凭据配置方式请参见：https://help.aliyun.com/document_detail/378657.html。
        com.aliyun.credentials.Client credential = new com.aliyun.credentials.Client();
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setCredential(credential);
        config.accessKeyId = accessKeyId;
        config.accessKeySecret = accessKeySecret;
        // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    public static SendSmsResponseBody sendSms(Map<String,String> map) throws Exception {
        if (ObjectUtil.isEmpty(map)){
            throw new ServiceException(GlobalErrorCodeConstants.PARAMETER_NOT_NULL);
        }
        ValidationUtils.isPhone(map.get("phoneNumber"));
        com.aliyun.dysmsapi20170525.Client client = AliyunSmsUtils.createClient();
        String code= RandomUtil.randomNumbers(4);
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setPhoneNumbers(map.get("phoneNumber"))
                .setTemplateParam(map.get("templateParam"));
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
            return sendSmsResponse.getBody();
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            return null;
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            System.out.println(error.getMessage());
            // 诊断地址
            System.out.println(error.getData().get("Recommend"));
            com.aliyun.teautil.Common.assertAsString(error.message);
            return null;
        }
    }
 /*   @NotNull
    private static StrongTextEncryptor textE() {
        StrongTextEncryptor textE = new StrongTextEncryptor();
        textE.setPassword(PS);
        return textE;
    }*/
}
